package Components
{
	import Helpers.ProximityEvent;
	import Helpers.ProximityWatcher;
	
	import Model.Task;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.binding.utils.ChangeWatcher;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.UIComponent;
	import mx.events.FlexMouseEvent;
	import mx.managers.PopUpManager;

	[Event(name="ShowingPostIt", type="flash.events.Event")]
	public class TaskTab extends UIComponent implements IListItemRenderer
	{
		[Bindable]
		public var task:Task;
		public var rack:TaskRack;
		
		protected var postIt:PostIt;
		protected var proxWatch:ProximityWatcher;
				
		private var showingPostIt:Boolean = false;
		
		public function get data():Object
		{
			return task;
		}
		public function set data(val:Object):void
		{
			task = val as Task;
		}
		
		public function TaskTab()
		{
			super();
			this.maxWidth = 30;
			addEventListener(MouseEvent.MOUSE_OVER, showPostItHandler);
			ChangeWatcher.watch(this,["task","IsAttached"], attachedChangedHandler);
		}
		
		protected function attachedChangedHandler(evt:Event):void
		{
			if(!task.isAttached && proxWatch)
				proxWatch.unWatch();
		}
		protected override function createChildren():void
		{
			super.createChildren();
			if(!postIt)
			{
				postIt = new PostIt();
				postIt.task = task;
				postIt.addEventListener(FocusEvent.FOCUS_IN, function(devt:Event):void
				{
					proxWatch.unWatch();
				});
				postIt.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, function(devt:Event):void
				{
					HidePostIt();
				});
				postIt.addEventListener("DeleteTask", function(devt:Event):void
				{
					rack.removeData(task);
				});
			}
		}
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			graphics.clear();
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			graphics.beginFill(task.Color);
			graphics.drawRect(0, 0, unscaledWidth - 5, unscaledHeight);
			graphics.endFill();
		}
		
		public function ShowPostIt():void
		{
			if(!rack.showDetached && !task.isAttached || showingPostIt)
				return;			
			
			dispatchEvent(new Event("ShowingPostIt"));
			
			
			PopUpManager.addPopUp(postIt,this);
			
			var point:Point = new Point(0, unscaledHeight);
			point = localToGlobal(point);
			point = postIt.parent.globalToLocal(point);
			postIt.move(point.x, point.y);
			
			proxWatch = ProximityWatcher.watch(postIt, 30, proximityHandler);

			showingPostIt = true;
		}
		protected function proximityHandler(evt:ProximityEvent):void
		{
			if(evt.type == ProximityEvent.PROXIMITY_EXIT)
				HidePostIt();
		}
		
		public function HidePostIt():void
		{
			if(!showingPostIt)
				return;
			
			PopUpManager.removePopUp(postIt);
			proxWatch.unWatch();
			
			invalidateDisplayList();
			showingPostIt = false;
		}
		
		protected function hidePostItHandler(evt:Event):void
		{
			HidePostIt();
		}
		protected function showPostItHandler(evt:Event):void
		{
			ShowPostIt();
		}
	}
}